<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "gky_prize".
 *
 * @property string $id
 * @property string $projectname
 * @property string $prizename
 * @property string $prizeno
 * @property string $prizetype
 * @property string $prizedate
 * @property string $members
 * @property string $prizedept
 * @property string $prizelevel
 * @property string $mark
 * @property string $updatetime
 * @property integer $isfinish
 * @property string $addman
 * @property string $addmanid
 * @property string $addtime
 *
 * @property Dept $dept
 */
class Prize extends \yii\db\ActiveRecord {

    public $dname;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'gky_prize';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['projectname', 'prizename'], 'required'],
            [['prizeno', 'isfinish','members'], 'safe'],
            [['addmanid'], 'integer'],
            [['projectname', 'prizename'], 'string', 'max' => 255],
            [['prizetype'], 'string', 'max' => 128],
//            [['members'], 'string', 'max' => 512],
            [['prizedept'], 'string', 'max' => 126],
            [['prizelevel', 'addman'], 'string', 'max' => 64],
            [['mark'], 'string', 'max' => 1024],
            [['updatetime', 'addtime', 'prizedate'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'projectname' => '项目名称',
            'prizename' => '获奖名称',
            'prizeno' => '获奖编号',
            'prizetype' => '获奖类型',
            'prizedate' => '获奖日期',
            'members' => '项目负责人及成员',
            'prizedept' => '获奖部门',
            'prizelevel' => '获奖等级',
            'mark' => '备注',
            'updatetime' => '跟新时间',
            'isfinish' => '是否完成',
            'addman' => '添加人',
            'addmanid' => '添加人id',
            'addtime' => '添加时间',
        ];
    }

    /**
     * 获取同一个项目的所有成员
     */
    public function getMember() {
        return $this->hasMany(Member::className(), ['project_id' => 'id']);
    }

    /**
     * 获取获奖部门
     */
    public function getDept() {

        return $this->hasOne(Dept::className(), ['id' => 'prizedept']);
    }

    //在这里将 本项目的所有成员保存到project表中
    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            // Place your custom code here
            $membersId = Yii::$app->request->post('projectmember');
            if ($membersId != '') {
                for ($i = 0; $i < count($membersId); $i++) {
                    $members[$i] = GkyUser::find()->where(['id' => $membersId[$i]])->One()->username;
                }
                $this->members = implode(',', $members);
            }
            //添加修改人的信息 由于这部分没有必要自己填写 所以直接获取登录人的信息  在保存的时候设置就好
            $id = Yii::$app->user->identity->id;
            //添加人的id
            $this->addmanid = $id;
            //添加人的姓名
            $name = GkyUser::find()->where(['id' => $id])->one()->username;
            $this->addman = $name;
            //更新时间
            $time = date("Y-m-d");
            $this->updatetime = $time;
            //添加时间
            $this->addtime = $time;
            return true;
        } else {
            return false;
        }
    }


    /**
     * 查询获奖列表，返回字段为获奖名，部门，添加时间
     *
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getList($pageNow, $pageSize) {

        $list = Prize::find()
                ->joinWith('dept', true)
                ->addOrderBy(['addtime' => SORT_DESC])
                ->offset(($pageNow - 1) * $pageSize)
                ->limit($pageSize)
                ->all();
        $results = [];
        foreach ($list as $model) {
            $dname = '';
            if ($model->dept) {
                $dname = $model->dept->dname;
            } else {
                $dname = '未设置';
            }
            $result = [
                'id' => $model->id,
                'prizename' => $model->prizename,
                'dept' => $dname,
                'addtime' => $model->addtime
            ];
            $results[] = $result;
        }
        return $results;
    }

}
